package project.controllers.workers;

import java.util.ArrayList;
import java.util.GregorianCalendar;

import project.model.IGarage;
import project.model.IMaster;
import org.apache.log4j.Logger;

public class WorkerWithGarage {

	private static final String ALL_GARAGES_BUSY = "All garages busy";
	private static final String EMPTY_GARAGE = "Empty garage #";
	private static final String PLACE_ON_THIS_DATE = " empty places on this date";
	private static final Logger log = Logger.getLogger(WorkerWithGarage.class);

	static void getEmptyGarage(ArrayList<IGarage> garageList) {
		try {
			boolean temp = false;
			for (IGarage garage : garageList) {
				if (garage.getEmptyGarage()) {
					System.out.println(EMPTY_GARAGE + garage.getNumberGarage());
					temp = true;
				}
			}

			if (temp == false) {
				System.out.println(ALL_GARAGES_BUSY);
			}
		} catch (NullPointerException n) {
			log.warn("Collection is empty");

		}
	}

	static void getNumberEmptyPlaces(ArrayList<IMaster> masterList,
			ArrayList<IGarage> garageList, GregorianCalendar date) {
		try {
			ArrayList<IGarage> garageEmpty = new ArrayList<IGarage>();
			ArrayList<IMaster> masterFree = new ArrayList<IMaster>();

			for (IGarage garage : garageList) {
				if (garage.getOrder().getDeadlineDate().before(date)
						|| garage.getEmptyGarage()) {
					garageEmpty.add(garage);
				}
			}

			for (IMaster master : masterList) {
				if (master.getOrder().getDeadlineDate().before(date)
						|| master.getEmploymentMaster() == false) {
					masterFree.add(master);
				}
			}

			if (masterFree.size() > garageEmpty.size()) {
				System.out.println(garageEmpty.size() + PLACE_ON_THIS_DATE);
			} else if (masterFree.size() < garageEmpty.size()) {
				System.out.println(masterFree.size() + PLACE_ON_THIS_DATE);
			} else if (masterFree.size() == garageEmpty.size()) {
				System.out.println(garageEmpty.size() + PLACE_ON_THIS_DATE);
			}
		} catch (NullPointerException n) {
			log.warn("Collection is empty");

		}

	}

	static void addGarage(ArrayList<IGarage> garageList, IGarage garage) {
		try {
			garageList.add(garage);
		} catch (NullPointerException n) {
			log.warn("Collection is empty");

		}
	}

	static void deleteGarage(ArrayList<IGarage> garageList, IGarage garage) {
		try {
			ArrayList<IGarage> temp = new ArrayList<IGarage>();
			for (IGarage gar : garageList) {
				if (!gar.equals(garage)) {
					temp.add(gar);
				}
			}
			garageList.clear();
			garageList.addAll(temp);

		} catch (NullPointerException n) {
			log.warn("Collection is empty");

		}
	}
}
